﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HKEA.CMS.Core.IDAL.Tbl;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.DALFactory;
using HKEA.CMS.Core.Utility;

namespace HKEA.CMS.Core.BLL.Tbl
{
    public class tblCust_ContactPersonBLL
    {
        private ItblCust_ContactPerson IDAL;
        public tblCust_ContactPersonBLL()
        {
            IDAL = DataAccess.CreateItblCust_ContactPerson();
        }
        /// <summary>
        /// 创建对象
        /// </summary>
        /// <returns></returns>
        public static tblCust_ContactPersonBLL GetInstance()
        {
            return new tblCust_ContactPersonBLL();
        }
        /// <summary>
        /// 插入一条记录
        /// </summary>
        /// <param name="ent"></param>
        /// <returns></returns>
        public int InsertENT(tblCust_ContactPerson ent)
        {
            int ret = 0;
            if (ent != null)
            {
                try
                {
                    ret = IDAL.InsertENT(ent);
                }
                catch (Exception)
                {

                    throw;
                }
            }
            return ret;
        }


        /// <summary>
        /// 物理删除
        /// </summary>
        /// <param name="ent"></param>
        /// <returns></returns>
        public int DeleteENT(tblCust_ContactPerson ent)
        {
            int ret = 0;
            if (ent != null)
            {
                try
                {
                    ret = IDAL.DeleteENT(ent);
                }
                catch (Exception)
                {

                    throw;
                }
            }
            return ret;
        }
        /// <summary>
        /// 修改记录
        /// </summary>
        /// <param name="ent"></param>
        /// <returns></returns>
        public int UpdateENT(tblCust_ContactPerson ent)
        {
            int ret = 0;
            if (ent != null)
            {
                try
                {
                    ret = IDAL.UpdateENT(ent);
                }
                catch (Exception)
                {

                    throw;
                }
            }
            return ret;
        }
        /// <summary>
        /// 根据条件得到符合条件的结果集，带分页参数
        /// </summary>
        /// <param name="ent"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        public List<tblCust_ContactPerson> GetListByENT(tblCust_ContactPerson ent, PageParam param, string strPropertyName, string strOrderBy)
        {
            List<tblCust_ContactPerson> list = new List<tblCust_ContactPerson>();
            if (ent != null && param != null)
            {
                try
                {
                    list = IDAL.GetListByENT(ent, param, strPropertyName, strOrderBy);
                }
                catch (Exception)
                {
                    throw;
                }
            }
            return list;
        }

        public List<ContactPersonListDetail> GetCPListDetail(int uid)
        {
            List<ContactPersonListDetail> list = new List<ContactPersonListDetail>();
            if (uid > 0)
            {
                try { list = IDAL.GetCPListDetail(uid); }
                catch (Exception er)
                { throw; }
            }
            return list;
        }

        public List<M_ContactPersonListDetail> GetCPListDetail(List<int> ids, int custUID)
        {
            List<M_ContactPersonListDetail> list = new List<M_ContactPersonListDetail>();
            if (custUID > 0)
            {
                try { list = IDAL.GetCPListDetail(ids, custUID); }
                catch (Exception er) { throw; }
                return list;
            }
            return null;
        }

        /// <summary>
        /// 根据条件得到符合条件的结果集，不带分页参数
        /// </summary>
        /// <param name="ent"></param>
        /// <returns></returns>
        public List<tblCust_ContactPerson> GetListByENT(tblCust_ContactPerson ent)
        {
            List<tblCust_ContactPerson> list = new List<tblCust_ContactPerson>();
            if (ent != null)
            {
                try
                {
                    list = IDAL.GetListByENT(ent);
                }
                catch (Exception)
                {
                    throw;
                }
            }
            return list;
        }

        public bool IsExistCPInCustomer(tblCust_ContactPerson ent)
        {
            bool ret = false;
            if (ent != null)
            {
                if (ent.ContactPersonUID > 0 && ent.CustomerUID > 0)
                {
                    var temp = GetEntity(ent);
                    if (temp != null)
                    {
                        ret = true;
                    }
                }
            }
            return ret;
        }

        public tblCust_ContactPerson GetEntity(tblCust_ContactPerson ent)
        {
            tblCust_ContactPerson ret=new tblCust_ContactPerson();
            if (ent != null)
            {
                    ret = IDAL.GetListByENT(ent).FirstOrDefault();
            }
            return ret;
        }
        /// <summary>
        /// 根据条件得到符合条件的记录条数
        /// </summary>
        /// <param name="ent"></param>
        /// <returns></returns>
        public int GetCountByENT(tblCust_ContactPerson ent)
        {
            int ret = 0;
            if (ent != null)
            {
                try
                {
                    ret = IDAL.GetCountByENT(ent);
                }
                catch (Exception)
                {

                    throw;
                }
            }
            return ret;
        }
        /// <summary>
        /// 判断是否已存在符合条件的记录
        /// </summary>
        /// <param name="ent"></param>
        /// <returns></returns>
        public bool IsExist(tblCust_ContactPerson ent)
        {
            bool ret = false;
            if (ent != null)
            {
                if (GetCountByENT(ent) > 0)
                    ret = true;
            }
            return ret;
        }
    }
}
